package com.fenger.sys.controller;

import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.Dept;
import com.fenger.sys.domain.User;
import com.fenger.sys.service.DeptService;
import com.fenger.sys.service.DeptService;
import com.fenger.sys.utils.*;
import com.fenger.sys.vo.DeptVo;
import com.fenger.sys.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class DeptController {

    private final static String URL_MENU = "dept/";


    @Autowired
    DeptService deptService;

    @ResponseBody
    @RequestMapping(URL_MENU+"loadAllLeftDeptTree")
    public DtreeJson loadAllLeftDeptTree(DeptVo deptVo){
        HttpSession session = WebUtils.getCurrentSession();
        deptVo.setAvailable(SysConstast.AVAILABLE_TRUE);
        List<Dept> depts = null;
            depts=deptService.queryAllDept(deptVo);
        List<DtreeNode> dtreeNodes = new ArrayList<>();
        for (Dept dept : depts) {
            Integer id =dept.getId();
            String title = dept.getName();
            String checkArr = "0";
            Integer parentId = dept.getPid();
            Boolean spread = dept.getOpen()==SysConstast.STATUS_OPEN_TRUE?true:false;
            String iconClass = "";
            dtreeNodes.add(new DtreeNode(id,title,checkArr,parentId,spread,iconClass));
        }

        return new DtreeJson(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,dtreeNodes);
    }


    /**
     * 封装弹出层下拉树数据格式
     * @param deptVo
     * @return
     */
    @ResponseBody
    @RequestMapping(URL_MENU+"loadAllAccordionDeptTree")
    public DtreeJson loadAllAccordionDeptTree(DeptVo deptVo){
        HttpSession session = WebUtils.getCurrentSession();
//        deptVo.setAvailable(SysConstast.AVAILABLE_TRUE);
        List<Dept> depts = null;
        depts=deptService.queryAllDept(deptVo);
        List<DtreeNode> dtreeNodes = new ArrayList<>();
        for (Dept dept : depts) {
            Integer id =dept.getId();
            String title = dept.getName();
            String checkArr = "0";
            Integer parentId = dept.getPid();
            Boolean spread = dept.getOpen()==SysConstast.STATUS_OPEN_TRUE?true:false;
            String iconClass = "";
            dtreeNodes.add(new DtreeNode(id,title,checkArr,parentId,spread,iconClass));
        }
        List<DtreeNode> dtreeNodeList = DtreeNodeUtils.build(dtreeNodes, 0);

        return new DtreeJson(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,dtreeNodeList);
    }


    @ResponseBody
    @RequestMapping(URL_MENU+"loadAllDeptForTable")
    public DataGirdView loadAllDeptForTable(DeptVo deptVo){
        return this.deptService.queryAllDeptForPage(deptVo);
    }
    @ResponseBody
    @RequestMapping(URL_MENU+"loadMaxOrderNum")
    public Integer loadMaxOrderNum(){


        return this.deptService.queryMaxOrderNum();
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "addDept")
    private DataGirdView addDept(DeptVo deptVo) {
        deptVo.setCreatetime(new Date());
        HttpSession session = WebUtils.getCurrentSession();
        try {
            this.deptService.addDept(deptVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "updateDept")
    private DataGirdView updateDept(DeptVo deptVo) {
        try {
            this.deptService.updateDept(deptVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(URL_MENU + "deleteDept")
    private DataGirdView deleteDept(DeptVo deptVo) {
        try {
            this.deptService.deleteDept(deptVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }

@ResponseBody
    @RequestMapping(URL_MENU + "checkHasChildren")
    private DataGirdView checkHasChildren(DeptVo deptVo) {
        try {
            Boolean hasChildren = this.deptService.checkHasChildren(deptVo.getId());
            if(hasChildren){
                return new DataGirdView(SysConstast.HAS_CHILDREN_CODE, null, 1L, null);
            }
            return new DataGirdView(SysConstast.NOT_HAS_CHILDREN_CODE, null, 0L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.NOT_HAS_CHILDREN_CODE, null, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(URL_MENU + "deleteListDept")
    private DataGirdView deleteListDept(@RequestParam(value="id[]", required = false) String[] id) {
        for (int i = 0; i < id.length; i++) {
            String s = id[i];
            System.out.println(s);
        }
        return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
    }


}
